﻿using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;

namespace Monti.Docking
{
   #region Tab strip to row

   /// <summary>
   /// Converts the tab strip placement to a row number and back.
   /// </summary>
   public class TabStripPlacementRowConverter : IMultiValueConverter
   {
      /// <summary>
      /// Converts the given placement to a row number.
      /// </summary>
      /// <param name="value">Tab strip placement.</param>
      /// <param name="targetType">Required return type.</param>
      /// <param name="parameter">Additional data.</param>
      /// <param name="culture">CultureInfo</param>
      /// <returns>Row number need for tab strip placement.</returns>
      public object Convert (object[] value, Type targetType, object parameter, CultureInfo culture)
      {
         if (value.Length < 2) return 1;

         var isToolWindow = (bool)value[0];
         if (isToolWindow) return 3;

         int row = 1;
         if (value[1].GetType () == typeof (Dock))
         {
            var dockPos = (Dock)value[1];
            if (dockPos == Dock.Right || dockPos == Dock.Left)
               row = 2;
            else if (dockPos == Dock.Bottom)
               row = 3;
         }
         return row;
      }

      /// <summary>
      /// Converts the given row number to a tab strip placement.
      /// </summary>
      /// <param name="value">Row number.</param>
      /// <param name="targetType">Required return type.</param>
      /// <param name="parameter">Additional data.</param>
      /// <param name="culture">CultureInfo</param>
      /// <returns>Tab strip placement for row number.</returns>
      public object[] ConvertBack (object value, Type[] targetType, object parameter, CultureInfo culture)
      {
         throw new NotImplementedException ();
      }
   }

   #endregion

   #region Tab strip to column

   /// <summary>
   /// Converts the tab strip placement to a column number and back.
   /// </summary>
   public class TabStripPlacementColumnConverter : IValueConverter
   {
      /// <summary>
      /// Converts the given placement to a column number.
      /// </summary>
      /// <param name="value">Tab strip placement.</param>
      /// <param name="targetType">Required return type.</param>
      /// <param name="parameter">Additional data.</param>
      /// <param name="culture">CultureInfo</param>
      /// <returns>Column number need for tab strip placement.</returns>
      public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
      {
         // Default...
         int col = 1;
         if (value.GetType() == typeof(Dock))
         {
            var dockPos = (Dock)value;
            if (dockPos == Dock.Left)
               col = 0;
            else if (dockPos == Dock.Right)
               col = 2;
         }
         return col;
      }

      /// <summary>
      /// Converts the given column number to a tab strip placement.
      /// </summary>
      /// <param name="value">Column number.</param>
      /// <param name="targetType">Required return type.</param>
      /// <param name="parameter">Additional data.</param>
      /// <param name="culture">CultureInfo</param>
      /// <returns>Tab strip placement for column number.</returns>
      public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
      {
         // Default...
         Dock dockPos = Dock.Top;
         if (value is int)
         {
            var col = (int)value;
            // This is not completely right b/c column 1 could be dock top or bottom...
            if (col == 0)
               dockPos = Dock.Left;
            else if (col == 2)
               dockPos = Dock.Right;
         }
         return dockPos;
      }
   }

   #endregion
}
